package com.csun.nusing.ui.familyactivity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.NursingOldrestListJsonBean;
import com.csun.nusing.bean.jsonbean.NursingOldrestListJsonBean.NursingOldrestDataBean;
import com.csun.nusing.bean.jsonbean.NursingOldrestListJsonBean.NursingOldrestDataBean.NursingOldrestListBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.activity.SuiFangDeviceActivity;
import com.csun.nusing.ui.adapter.MyBaseAdapter;
import com.csun.nusing.ui.adapter.ViewHolder;
import com.csun.nusing.ui.myview.RoundImageView;
import com.csun.nusing.util.SPUtils;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.util.HashMap;
import java.util.Map;

/**
 * 随访仪老人选择 ClassName: FollowOldSelectActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-11-3
 */
public class FamilyFollowOldSelectActivity extends
		BaseActivity<CommonPresenter> {

	private ListView lv;
	private String authorization;
	private String userid;
	protected NursingOldrestListJsonBean nursingoldrestlistbean;
	protected TextView nametv;
	protected TextView phonetv;
	protected Intent intent;
	private String deviceId;
	private String deviceMacAddress;

	@Override
	public int getContentViewId() {
		return R.layout.activity_family_follow_olderselet_lv;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		initView();
	}

	/**
	 * 初始化view
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	private void initView() {
		lv = findViewById(R.id.activity_nursing_follow_oldrest_lv);
	}

	@Override
	public void initData() {
		super.initData();
		// 请求老人列表
		authorization = (String) SPUtils.get(this, "authorization", "");
		userid = (String) SPUtils.get(this, "userId", "");
		deviceId = getIntent().getStringExtra("deviceId");
		deviceMacAddress = getIntent().getStringExtra("deviceMacAddress");
		Map<String, String> param = new HashMap<String, String>();
		param.put("userid", "" + userid);
		param.put("pageNo", "1");
		param.put("pageSize", "100");

		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "-------" + result);
							Gson gson = new Gson();
							nursingoldrestlistbean = gson.fromJson(result,
									NursingOldrestListJsonBean.class);
							if (nursingoldrestlistbean != null
									&& nursingoldrestlistbean.getData() != null) {
								controllistView(nursingoldrestlistbean
										.getData());
							}
						}
					}
				}).start(UrlBean.NURSINGBYOLDMANURL);
	}

	/**
	 * 加载数据
	 * 
	 * @Description: TODO
	 * @param @param nursingOldrestDataBean
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	protected void controllistView(NursingOldrestDataBean nursingOldrestDataBean) {
		if (nursingOldrestDataBean.getList() == null) {
			return;
		}
		if (nursingOldrestDataBean.getList().size() == 0) {
			return;
		}
		if (nursingOldrestDataBean.getList().get(0) == null) {
			Log.e("fzq", ""+nursingOldrestDataBean.getList().get(0).toString());
			return;
		}
		MyBaseAdapter<NursingOldrestListBean> myBaseAdapter = new MyBaseAdapter<NursingOldrestListBean>(
				this, nursingOldrestDataBean.getList(),
				R.layout.item_nursing_follow_older_list) {
			@Override
			public void convert(ViewHolder helper, int position,
					NursingOldrestListBean item) {
				RoundImageView imageView = helper
						.getView(R.id.item_nursingoldrest_list_header_iv);
				nametv = helper
						.getView(R.id.item_nursingoldrest_list_state_tv);
				phonetv = helper
						.getView(R.id.item_nursingoldrest_list_phone_tv);

				Glide.with(FamilyFollowOldSelectActivity.this)
						.load(item.getHeadPicture()).into(imageView);
				nametv.setText("" + item.getName());
				phonetv.setText("" + item.getPhone());
			}
		};
		lv.setAdapter(myBaseAdapter);
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				intent = new Intent(FamilyFollowOldSelectActivity.this,
						SuiFangDeviceActivity.class);
				intent.putExtra("olderId", nursingoldrestlistbean.getData()
						.getList().get(position).getId());
				intent.putExtra("deviceMacAddress", deviceMacAddress);
				startActivity(intent);
			}
		});
	}

}
